import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:wanAndroid/widget/BaseLoadWidget.dart';

import 'ListBaseWidget.dart';

class LoadMore extends BaseLoadState {
  WidgetsBinding widgetsBinding;
  final LoadCallback onLoad; //错误事件处理;;

//定义一个key
  GlobalKey _key = GlobalKey();

  LoadMore(this.onLoad);

  @override
  void initState() {
    widgetsBinding = WidgetsBinding.instance;
    widgetsBinding.addPostFrameCallback(_getRenderBox);
    super.initState();
  }

  _getRenderBox(_) {
    //获取`RenderBox`对象
    RenderBox renderBox = _key.currentContext.findRenderObject();
    Offset offset = renderBox.localToGlobal(Offset.zero);

    Size size = MediaQuery.of(_key.currentContext).size;
    //如果屏幕的高度大于当前widget 的屏幕底部说明没有超过一屏
    if (size.height > offset.dy) {
      setState(() {
        status = LoadMoreStatus.NONE;
      });
    } else {
      setState(() {
        status = LoadMoreStatus.LOADING;
      });
      if (onLoad != null) {
        onLoad();
      }
    }
  }

  @override
  Widget endWidget(BuildContext context) {
    return new Container(
      color: const Color(0xFFEEEEEE),
      padding: const EdgeInsets.fromLTRB(5.0, 15.0, 5.0, 15.0),
      child: new Row(
        children: <Widget>[
          new Expanded(
            child: new Divider(
              height: 10.0,
            ),
            flex: 1,
          ),
          new Text(
            "我是有底线的",
            style: new TextStyle(color: Theme.of(context).accentColor),
          ),
          new Expanded(
            child: new Divider(
              height: 10.0,
            ),
            flex: 1,
          ),
        ],
      ),
    );
  }

  @override
  Widget loadWidget(BuildContext context) {
    return ListTile(
        key: _key,
        title: Center(
          child: new Center(
            child: new CircularProgressIndicator(),
          ),
        ));
  }

  @override
  Widget errorWidget(BuildContext context) {
    return ListTile(
        title: Center(
          child: Text(
            "加载失败",
          ),
        ),
        onTap: () {
          setState(() {
            status = LoadMoreStatus.LOADING;
          });
          onLoad();
        });
  }
}
